#python -m data.api_data.api_monitor   文件直接执行代码
from .api_token import get_id,get_time,get_token
from util.time_util import timestran
import requests
import json
with open(r'data\json_data\monitor.json','r',encoding='utf-8') as f:
    data = json.load(f)['api']

def get_devicestate():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/realtime/device'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'device_id': 57714}
    response = requests.post(url=url,headers=headers,json=json)
    for alarm in response.json()['data']['alarm_infos']:
        for key in data['devicesate']:
            if key == 'hase_alarm':
                if alarm[key] == 0:                
                    values.append('正常')
                elif alarm[key] == 1:
                    values.append('异常')
            else:
                values.append(alarm[key])
    return values
def get_testbits():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/realtime/device'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'device_id': 57714}
    response = requests.post(url=url,headers=headers,json=json)
    for site in response.json()['data']['vals']:
        values.append(site['name'])
        values.append(site['val'])
    return values
def get_alarmlist():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/list'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'device_id':57714,'limit':6,'page':1}
    response = requests.post(url=url,headers=headers,json=json)
    for elm in response.json()['data']['list']:
        for key in data['alarmlist']:
            if key == 'alarm_at':
                for i in timestran(elm[key]).split(' '):
                    values.append(i)
            elif key == 'level':
                if elm[key] == 1:
                    values.append('预警')
                elif elm[key] ==2:
                    values.append('报警')
            elif key == 'return_status':
                if elm[key] == 1:
                    values.append('已复归')
                else:
                    values.append('未复归')
            else:
                values.append(elm[key]) 
    return values
